package Events;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;

import AI.Crew.Crew;
import Announcements.Announcement;
import Constructions.TeleportPad;
import FrameWork.Menu;
import Main.SpaceGame;
import Menus.HireMenu;

public class CrewArrival implements Event
{
	private static final Random rand = new Random();
	private int duration, wait;
	public ArrayList<Crew> entities = new ArrayList<Crew>();
	private TeleportPad teleportPad;
	private HireMenu menu;

	public void update(SpaceGame main) 
	{	
		if (wait <= 35)
			teleportPad.drawTeleport(this);
		if (wait > -1)
			wait--;
		else
		{
			int crewCnt = 0;
			for(Crew c:entities)
			{
				main.aiManager.entityList.add(c);
				if (crewCnt%2 != 0)
					c.setPos(teleportPad.getX()+((int)(crewCnt/2+.5)), teleportPad.getY());
				else
					c.setPos(teleportPad.getX()+((int)(crewCnt/2+.5)), teleportPad.getY()+1);
				if (crewCnt >= 5)
					crewCnt = 1;
				else crewCnt++;
			}
			main.announcementManager.AddAnnouncement(new Announcement("Fresh crew have arrived!", Color.green));
			for(Menu m:main.menuManager.menuList)
			{
				if(m instanceof HireMenu && ((HireMenu)m).equals(menu))
				{
					((HireMenu)m).hireList.clear();
				}
			}
		}
		setDuration(getDuration() - 1);
	}

	public void init(SpaceGame main) 
	{
		wait = rand.nextInt(500) + 250;
		setDuration(wait+1);
		this.teleportPad = main.constructionManager.getTeleportPad();
	}

	public boolean isDone()
	{
		return getDuration()<0;
	}

	public int getDuration() 
	{
		return duration;
	}
	
	public void setMenu(HireMenu menu)
	{
		this.menu = menu;
	}

	public void setDuration(int duration) 
	{
		this.duration = duration;
	}
	
}
